package com.vitabuybuy.tv.ui.home.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.*
import com.vitabuybuy.tv.network.AddressRepository
import com.vitabuybuy.tv.network.HomeRepository
import com.vitabuybuy.tv.network.ShopCenterRepository
import okhttp3.internal.notify
import rxhttp.async

class HomeViewModel : BaseViewModel() {

    val homeData = MutableLiveData<HomeDataDto>()
    private var regionId: Int? = -1

    override fun onRetry() {
        getHomeData(regionId, true)
    }

    fun getTabHomeData(position: Int) {
        val homeDataTemp = homeData.value
        rxHttpRequest {
            onRequest = {
                when (position) {
                    0 -> {
                        homeDataTemp?.goodlist =
                            HomeRepository.getHomeGoodList(createGoodsRequestParams(regionId))
                                .await()
                    }
                    1 -> {
                        homeDataTemp?.livelist= HomeRepository.getLiveList(createNormalRequestParams(regionId)).await()
                    }
                    2 -> {
                        homeDataTemp?.groupGoodlist= HomeRepository.getGroupGoodsList(createNormalRequestParams(regionId))
                            .await()
                    }
                    3 -> {
                        homeDataTemp?.biddingGoodlist= HomeRepository.getBiddingGoodsList(createBiddingRequestParams(regionId))
                            .await()
                    }
                }
                homeData.value = homeDataTemp
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun getHomeData(regionId: Int?, isRefresh: Boolean) {
        this.regionId = regionId
        rxHttpRequest {
            onRequest = {
                val value1 =
                    HomeRepository.getHomeGoodList(createGoodsRequestParams(regionId)).async(this)
                val value2 = HomeRepository.getGroupGoodsList(createNormalRequestParams(regionId))
                    .async(this)
                val value3 =
                    HomeRepository.getBiddingGoodsList(createBiddingRequestParams(regionId))
                        .async(this)
                val value4 =
                    HomeRepository.getLiveList(createNormalRequestParams(regionId)).async(this)
                val value5 = HomeRepository.getBannerList().async(this)
                val value6 = ShopCenterRepository.getFirstCategory().async(this)

                homeData.value = HomeDataDto(
                    goodlist = value1.await(),
                    groupGoodlist = value2.await(),
                    biddingGoodlist = value3.await(),
                    livelist = value4.await(),
                    bannerlist = value5.await(),
                    categoryList = value6.await()
                )
                statusView.set(LoadingType.SHOW_CONTENT)
            }
            onError = {
                XToast.normal(it.msg)
            }
            this.isRefresh = isRefresh
            isLoadingType = true
        }
    }

    private fun createGoodsRequestParams(regionId: Int?): HashMap<String, Any> {
        val params = HashMap<String, Any>()
        if (regionId != -1) params["region"] = mapOf(Pair("id", regionId))
        params["product"] = mapOf(Pair("featured", true))
        params["pageable"] = mapOf(
            Pair("page", 1),
            Pair("size", 20),
        )
        return params
    }

    private fun createNormalRequestParams(regionId: Int?): HashMap<String, Any> {
        val params = HashMap<String, Any>()
        if (regionId != -1) params["region"] = mapOf(Pair("id", regionId))
        params["pageable"] = mapOf(
            Pair("page", 1),
            Pair("size", 20),
            Pair("desc", "id")
        )
        return params
    }

    private fun createBiddingRequestParams(regionId: Int?): HashMap<String, Any> {
        val params = HashMap<String, Any>()
        if (regionId != -1) params["region"] = mapOf(Pair("id", regionId))
        params["status"] = "进行中"
        params["pageable"] = mapOf(
            Pair("page", 1),
            Pair("size", 20),
            Pair("desc", "id")
        )
        return params
    }


//    val goodlist = MutableLiveData<List<GoodsDto>>()
//
//    fun getNormalGoodsList(categoryId: Int, regionId: Int?) {
//        val params = HashMap<String, Any>()
//        if (categoryId != -1) params["category"] = mapOf(Pair("id", categoryId))
//        if (regionId != -1) params["region"] = mapOf(Pair("id", regionId))
//        params["product"] = mapOf(Pair("featured", true))
//        params["pageable"] = mapOf(
//            Pair("page", 1),
//            Pair("size", 20)
//        )
//        rxHttpRequest {
//            onRequest = {
//                goodlist.value = HomeRepository.getHomeGoodList(params).await()
//                defUI.msgEvent.postValue(MessageModel(1))
//            }
//            onError = {
//                XToast.normal(it.msg)
//            }
//        }
//    }
//
//    val groupGoodlist = MutableLiveData<List<GroupBuyListDto>>()
//    fun getGroupGoodsList(categoryId: Int, regionId: Int?) {
//        val params = HashMap<String, Any>()
//        if (categoryId != -1) params["category"] = mapOf(Pair("id", categoryId))
//        if (regionId != -1) params["region"] = mapOf(Pair("id", regionId))
//        params["pageable"] = mapOf(
//            Pair("page", 1),
//            Pair("size", 20),
//            Pair("desc", "id")
//        )
//        rxHttpRequest {
//            onRequest = {
//                groupGoodlist.value = HomeRepository.getGroupGoodsList(params).await()
//                defUI.msgEvent.postValue(MessageModel(1))
//            }
//            onError = {
//                XToast.normal(it.msg)
//            }
//        }
//    }
//
//    val biddingGoodlist = MutableLiveData<List<BiddingListDto>>()
//    fun getBiddingGoodsList(categoryId: Int, regionId: Int?, status: String) {
//        val params = HashMap<String, Any>()
//        params["status"] = status
//        if (regionId != -1) params["region"] = mapOf(Pair("id", regionId))
//        if (categoryId != -1) params["category"] = mapOf(Pair("id", categoryId))
//        params["pageable"] = mapOf(
//            Pair("page", 1),
//            Pair("size", 20),
//            Pair("desc", "id")
//        )
//        rxHttpRequest {
//            onRequest = {
//                biddingGoodlist.value = HomeRepository.getBiddingGoodsList(params).await()
//                defUI.msgEvent.postValue(MessageModel(1))
//            }
//            onError = {
//                XToast.normal(it.msg)
//            }
//        }
//    }
//
//    val livelist = MutableLiveData<MutableList<LiveListDto>>()
//
//    fun getLiveList(
//        categoryId: Int,
//        regionId: Int?
//    ) {
//        val params = HashMap<String, Any>()
//        if (regionId != -1) params["region"] = mapOf(Pair("id", regionId))
//        if (categoryId != -1) params["category"] = mapOf(Pair("id", categoryId))
//        params["pageable"] = mapOf(
//            Pair("page", 1),
//            Pair("size", 20),
//            Pair("desc", "id")
//        )
//        rxHttpRequest {
//            onRequest = {
//                livelist.value = HomeRepository.getLiveList(params).await()
//                defUI.msgEvent.postValue(MessageModel(1))
//            }
//            onError = {
//                XToast.normal(it.msg)
//            }
//        }
//    }
//
//    val bannerlist = MutableLiveData<BannerDto>()
//    fun getBannerList() {
//        rxHttpRequest {
//            onRequest = {
//                bannerlist.value = HomeRepository.getBannerList().await()
//            }
//            onError = {
//                XToast.normal(it.msg)
//            }
//        }
//    }

    fun getRegionList(regionId: Int?) {
        rxHttpRequest {
            onRequest = {
                val it = AddressRepository.getRegionList(regionId).await()
                defUI.msgEvent.postValue(MessageModel(1, obj = it))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
